package display.android.fyales.com.displaytime;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.time.Clock;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private TextView mTimeTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this,ScreenService.class);
        startService(intent);

        mTimeTv = (TextView) findViewById(R.id.time_tv);
    }

    @Override
    protected void onStart() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ScreenService.getInstance() != null){
                    ScreenService.getInstance().setCloseTime();
                    setTime();
                }
            }
        },500);
        super.onStart();
    }

    private void setTime(){
        if (ScreenService.getInstance() != null){
            HashMap<String,String> maps = ClockUtil.setClockInfo(ScreenService.getInstance().getAllTime());
            if (maps != null){
                mTimeTv.setText("已亮屏" + maps.get(ClockUtil.CLOCK_HOUR) + "时" +
                        maps.get(ClockUtil.CLOCK_MINUTE) + "分" +
                        maps.get(ClockUtil.CLOCK_SECOND) + "秒");
            }

        }
    }
}
